package mincostmaxflow;

import com.Ostermiller.util.CSVParser;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class GraphFileParser {

    private static Graph<Edge> result;

    public static Graph<Edge> readFile(String fName) throws FileNotFoundException, IOException, Exception{
        FileInputStream fis = new FileInputStream(fName);
        CSVParser csvp = new CSVParser(fis);
        csvp.setCommentStart("#@!");

	result = new Graph<Edge>();

        String[] csv_line;
        while((csv_line = csvp.getLine()) != null){
            if(csv_line[0].contains("Source")){
                for(int i = 1; i < csv_line.length; i++)
                    result.addSourceVertex(Integer.parseInt(csv_line[i]));
            }
            else if(csv_line[0].contains("Drain")){
                for(int i = 1; i < csv_line.length; i++)
                    result.addDrainVertex(Integer.parseInt(csv_line[i]));
            }
            else{
                if(csv_line.length == 4)
                    result.add(new Edge(
                            //edge start vertice
                            Integer.parseInt(csv_line[0]),
                            //edge end vertice
                            Integer.parseInt(csv_line[1]),
                            //edge cost
                            Integer.parseInt(csv_line[2]),
                            //edge capacity
                            Integer.parseInt(csv_line[3])));
                else
                    throw new IllegalArgumentException("Wrong number of values to create an Edge" +
                            "on line " + csvp.getLastLineNumber() + " of the CSV file!");
            }
        }
	return result;
    }



}
